# coding:utf-8

import time
import os
import multiprocessing


# def work_a():
#     for i in range(5):
#         print(i,'a',os.getpid())
#         time.sleep(1)
#
# def work_b():
#     for i in range(5):
#         print(i,'b',os.getpid())
#         time.sleep(1)
#
#
# if __name__ == '__main__':
#     start = time.time()
#     a_p = multiprocessing.Process(target=work_a)
#     # a_p.start()
#     # a_p.join()    #主进程阻塞
#     b_p = multiprocessing.Process(target=work_b)
#     # b_p.start()
#     # b_p.join()
#     for p in (a_p,b_p):
#         p.start()
#
#     for p in (a_p,b_p):
#         p.join()
#
#     for p in (a_p,b_p):
#         print(p.is_alive())
#
#     print('时间消耗:',time.time()-start)
#     print('parent pid is  %s' % os.getpid())
#=====================================================

# def work(count,lock):
#     lock.acquire()
#     print(count,os.getpid())
#     time.sleep(5)
#     lock.release()
#     return 'result is %s,pid is %s' % (count,os.getpid())
#
# if __name__ == '__main__':
#     pool = multiprocessing.Pool(5)
#     manager = multiprocessing.Manager()
#     lock = manager.Lock()
#     results =[]
#     for i in range(10):
#         result = pool.apply_async(func=work,args=(i,lock))
#         results.append(result)
#     #
#     # for res in results:
#     #     print(res.get())
#
#     pool.close()
#     pool.join()
#====================================================================
# import json
#
# class Work(object):
#     def __init__(self,q):
#         self.q = q
#
#     def send(self,message):
#         if not isinstance(message,str):
#             message = json.dumps(message)
#         self.q.put(message)
#
#     def send_all(self):
#         for i in range(20):
#             self.q.put(i)
#             time.sleep(1)
#
#     def receive(self):
#         while 1:
#             result = self.q.get()
#             try:
#                 res = json.loads(result)
#             except:
#                 res = result
#             print('recv is %s' % res)
#
# if __name__ == '__main__':
#     q = multiprocessing.Queue()
#     work = Work(q)
#     send = multiprocessing.Process(target=work.send,args=({'name':'xiaomu'},))
#     recv = multiprocessing.Process(target=work.receive)
#     send_all_p = multiprocessing.Process(target=work.send_all)
#
#     send.start()
#     recv.start()
#     send_all_p.start()
#
#     send.join()
#     send_all_p.join()
#     # recv.terminate()
#====================================================

# import random
# import time
# import threading
#
# lists = ['python', 'django', 'tornado',
#          'flask', 'bs5', 'requests', 'uvloop'
# ]
#
# new_lists = []
#
#
# def work():
#     if len(lists) == 0:
#         return
#     data = random.choice(lists)
#     lists.remove(data)
#     new_data = '%s_new' % data
#     new_lists.append(new_data)
#     time.sleep(1)
#
#
# if __name__ == '__main__':
#     start = time.time()
#     print('old list len:', len(lists))
#     t_list = []
#     for i in range(len(lists)):
#         t = threading.Thread(target=work)
#         t_list.append(t)
#         t.start()
#
#     for t in t_list:
#         t.join()
#
#     print('old list:', lists)
#     print('new list:', new_lists)
#     print('new_list len', len(new_lists))
#     print('time is %s' % (time.time() - start))

#========================================================================
# import time
# import os
# import threading
#
# from concurrent.futures import ThreadPoolExecutor
#
#
# lock = threading.Lock()
#
#
# def work(i):
#     # lock.acquire()
#     print(i, os.getpid())
#     time.sleep(1)
#     # lock.release()
#     return 'result %s' % i
#
#
# if __name__ == '__main__':
#     print(os.getpid())
#     t = ThreadPoolExecutor(2)
#     result = []
#     for i in range(20):
#         t_result = t.submit(work, (i, ))
#         result.append(t_result)
#
#     for res in result:
#         print(res.result())

# =========================================================================

import time
import os
import random
import asyncio
import gevent
import multiprocessing
import threading
#
def gevent_a():
    for i in range(5):
        print(i, 'a gevent', os.getpid())
        gevent.sleep(random.random() * 2)
    return 'gevent a result'


def gevent_b():
    for i in range(5):
        print(i, 'b gevent', os.getpid())
        gevent.sleep(random.random() * 2)
    return 'b gevent result'



async def a():
    for i in range(5):
        print(i, 'a', os.getpid())
        await asyncio.sleep(random.random() * 2)
    return 'a function'

def a1():
    for i in range(5):
        print(i, 'a', os.getpid())
        time.sleep(random.random() * 2)
    return 'a1 function'

async def b():
    for i in range(5):
        print(i, 'b', os.getpid())
        await asyncio.sleep(random.random() * 2)
    return 'b function'

def b1():
    for i in range(5):
        print(i, 'b', os.getpid())
        time.sleep(random.random() * 2)
    return 'b1 function'

async def main():
    result = await asyncio.gather(
        a(),
        b()
    )
    print(result[0], result[1])


if __name__ == '__main__':
    start = time.time()
    # a_p = multiprocessing.Process(target=a1)
    # a_p.start()
    # a_p.join()
    # b_p = multiprocessing.Process(target=b1)
    # b_p.start()
    # b_p.join()

    # a_x = threading.Thread(target=a1)
    # a_x.start()
    # a_x.join()
    # b_x = threading.Thread(target=b1)
    # b_x.start()
    # b_x.join()

    # asyncio.run(main())
#
    g_a = gevent.spawn(gevent_a)
    # g_a.run()
    g_b = gevent.spawn(gevent_b)
    gevent_list = [g_a, g_b]
    result = gevent.joinall(gevent_list)
    print(result[0].value)
    print(result[1].value)
    print(time.time() - start)
    print('parent is %s' % os.getpid())

